package Service;

import model.NewRegular;
import model.Yangzhou;
import org.apache.ibatis.session.SqlSession;
import utils.MybatisUtil;

import java.util.ArrayList;
import java.util.List;

public class GetOriginalInfo implements AllOperating {

    public List<Yangzhou> getAll() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        List<Yangzhou> list = new ArrayList<Yangzhou>();
        try {
            list = sqlSession.selectList("selectAll");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(sqlSession);
        }
        return list;
    }

    public boolean testConnecting() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        List<Yangzhou> list = new ArrayList<Yangzhou>();
        try {
            list = sqlSession.selectList("testConnect");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(sqlSession);
        }
        return list.size() != 0;
    }

    public void update(Yangzhou yangzhou) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            sqlSession.update("updateBeforeGetMsg", yangzhou);
            sqlSession.commit();
            System.out.println("执行了");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(sqlSession);
        }
    }

    public void insertDailyMsg(List<NewRegular> list) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            sqlSession.insert("insertDailyMsg", list);
            sqlSession.commit();
            System.out.println("执行成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(sqlSession);
        }
    }

    public void deleteSpecifiedData(String date) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            sqlSession.delete("deleteDateData", date);
            sqlSession.commit();
            System.out.println("执行成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSession(sqlSession);
        }
    }
}
